package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.DepartmentMapper;
import com.usian.mapper.EmployeeMapper;
import com.usian.pojos.Department;
import com.usian.pojos.Employee;
import com.usian.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Department> findDepartment() {

        List<Department> list = list();
        return list;
    }

    @Override
    public void addDepartment(Department department) {

        save(department);
    }

    @Override
    public void updateDepartment(Department department) {

        updateById(department);
    }

    @Override
    public void delDepartment(Integer id) {

        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(Employee::getDid,id);

        Integer count = employeeMapper.selectCount(wrapper);

        if (count == 0){

            removeById(id);
        }


    }
}
